# encoding = utf-8
class User < ActiveRecord::Base
  has_one :info
  has_many :grants
  has_many :vouchers, :through => :grants
  has_many :carts
  has_many :deals
  has_many :feedbacks
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable, :omniauthable, :encryptable, :confirmable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

#  after_create :set_vouchers
#
#  def set_vouchers
#    vou = Voucher.where(:money => 5).first
#    if !vou.nil?
#      Grant.create({:user_id => self.id, :voucher_id => vou.id})
#    end
#  end
  
  def verified?
    if self.info.nil?
      return false
    else
      return self.info.verified
    end
  end

  def verified_show
    if self.verified?
      '认证成功'
    else
      '未认证'
    end
  end

  def confirmed_show
    if self.confirmed_at.nil?
      '否'
    else
      '是'
    end
  end

  def name(show = '未填写')
    if !self.info.nil? and !self.info.name.nil?
      self.info.name
    else
      show
    end
  end
  
  def gender
    if !self.info.nil? and !self.info.gender.nil?
      if self.info.gender == '0'
        '女'
      else
        '男'
      end
    else
      '未填写'
    end
  end

  def phone
    if !self.info.nil? and !self.info.phone.nil?
      self.info.phone
    else
      '未填写'
    end
  end

  def address
    if !self.info.nil?
      info = self.info
      building = info.building
      info.company_name + ' - ' + building.address + ' - ' + building.name+info.tower!+info.floor + '层'
    else
      '未填写'
    end
  end
end
